#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QClipboard>
#include "pointsgenerator.h"
#include "rotation.h"
#include "settings/settings.h"

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	clipboard = QApplication::clipboard();
	settings = new SettingsDialog;
	QObject::connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(sActionExitTriggered()));
	QObject::connect(ui->actionSettings,SIGNAL(triggered()),this,SLOT(sActionSettingsTriggered()));
	QObject::connect(ui->actionSaveFrame,SIGNAL(triggered()),this,SLOT(sActionSaveFrameTriggered()));
	QObject::connect(ui->actionSaveClipboard,SIGNAL(triggered()),this,SLOT(sActionSaveClipboardTriggered()));
	QObject::connect(ui->startButton,SIGNAL(clicked()),this,SLOT(sButtonStartClicked()));
	QObject::connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(sButtonStopClicked()));
	QObject::connect(ui->previousFrameButton,SIGNAL(clicked()),this,SLOT(sButtonPreviousClicked()));
	QObject::connect(ui->nextFrameButton,SIGNAL(clicked()),this,SLOT(sButtonNextClicked()));
	QObject::connect(ui->xSlider, SIGNAL(valueChanged(int)), Settings::instance(), SLOT(setKA(int)));
	QObject::connect(ui->ySlider, SIGNAL(valueChanged(int)), Settings::instance(), SLOT(setKB(int)));
	QObject::connect(ui->zSlider, SIGNAL(valueChanged(int)), Settings::instance(), SLOT(setKC(int)));
	QObject::connect(this, SIGNAL(onStart()), PointsGenerator::instance(), SLOT(sStart()));
	QObject::connect(this, SIGNAL(onStop()), PointsGenerator::instance(), SLOT(sStop()));
	QObject::connect(this, SIGNAL(onSceneRepaint()), ui->scene, SLOT(sSceneRepaint()));
	
	QObject::connect(ui->xSlider, SIGNAL(valueChanged(int)), this, SLOT(sChangeDegree(int)));
	QObject::connect(ui->ySlider, SIGNAL(valueChanged(int)), this, SLOT(sChangeDegree(int)));
	QObject::connect(ui->zSlider, SIGNAL(valueChanged(int)), this, SLOT(sChangeDegree(int)));
	
	PointsGenerator::instance()->sPrepare();
}

MainWindow::~MainWindow()
{
	QObject::disconnect(ui->actionExit,SIGNAL(triggered()),this,SLOT(sActionExitTriggered()));
	QObject::disconnect(ui->actionSettings,SIGNAL(triggered()),this,SLOT(sActionSettingsTriggered()));
	QObject::disconnect(ui->actionSaveFrame,SIGNAL(triggered()),this,SLOT(sActionSaveFrameTriggered()));
	QObject::disconnect(ui->actionSaveClipboard,SIGNAL(triggered()),this,SLOT(sActionSaveClipboardTriggered()));
	QObject::disconnect(ui->startButton,SIGNAL(clicked()),this,SLOT(sButtonStartClicked()));
	QObject::disconnect(ui->stopButton,SIGNAL(clicked()),this,SLOT(sButtonStopClicked()));
	QObject::disconnect(ui->previousFrameButton,SIGNAL(clicked()),this,SLOT(sButtonPreviousClicked()));
	QObject::disconnect(ui->nextFrameButton,SIGNAL(clicked()),this,SLOT(sButtonNextClicked()));
	QObject::disconnect(this, SIGNAL(onStart()), PointsGenerator::instance(), SLOT(sStart()));
	QObject::disconnect(this, SIGNAL(onStop()), PointsGenerator::instance(), SLOT(sStop()));
	QObject::disconnect(this, SIGNAL(onSceneRepaint()), ui->scene, SLOT(sSceneRepaint()));
	QObject::disconnect(ui->xSlider, SIGNAL(valueChanged(int)), Settings::instance(), SLOT(setKA(int)));
	QObject::disconnect(ui->ySlider, SIGNAL(valueChanged(int)), Settings::instance(), SLOT(setKB(int)));
	QObject::disconnect(ui->zSlider, SIGNAL(valueChanged(int)), Settings::instance(), SLOT(setKC(int)));
	QObject::disconnect(ui->xSlider, SIGNAL(valueChanged(int)), this, SLOT(sChangeDegree(int)));
	QObject::disconnect(ui->ySlider, SIGNAL(valueChanged(int)), this, SLOT(sChangeDegree(int)));
	QObject::disconnect(ui->zSlider, SIGNAL(valueChanged(int)), this, SLOT(sChangeDegree(int)));
    clipboard->clear();
    delete settings;
    delete ui;
}

void MainWindow::sActionExitTriggered()
{
	settings->close();
    this->close();
}

void MainWindow::sActionSaveClipboardTriggered()
{
    emit onStop();
    QImage save_image = ui->scene->getImage();
    clipboard->setImage(save_image);
    emit onStart();
}

void MainWindow::sActionSaveFrameTriggered()
{
	emit onStop();
    QImage save_image = ui->scene->getImage();

    if (!save_image.isNull ())
	{
		QString fileName = QFileDialog::getSaveFileName (this, tr ("Save Image"), QDir::currentPath(), tr ("Images (*.png *.jpg)"));

		if (!fileName.isEmpty ())
		{
			if (!save_image.save (fileName))
			{
//                QMessageBox::critical (this, tr ("Save Image Error"), tr ("Cannot save image: '%1'").arg (fileName));
			}
		}
	}
	emit onStart();
}

void MainWindow::sActionSettingsTriggered()
{
    settings->setVisible(true);
    if(settings->exec())
    {
    }
}

void MainWindow::sButtonStartClicked()
{
	PointsGenerator::instance()->sStart();
}

void MainWindow::sButtonStopClicked()
{
	PointsGenerator::instance()->sStop();
}

void MainWindow::sButtonPreviousClicked()
{
    PointsGenerator::instance()->selectFrame(-5);
    emit onStop();
}

void MainWindow::sButtonNextClicked()
{
    PointsGenerator::instance()->selectFrame(5);
    emit onStop();
}
void MainWindow::sChangeDegree(int value)
{
	if(!PointsGenerator::instance()->isStarted()){
		PointsGenerator::instance()->selectFrame(-1);
	}
}

